const jwt = require('jsonwebtoken')
const { PRIVATE_KEY } = require('../config/screct')
class LoginController {
    sign(ctx) {
        // 1.获取用户数据
        const { id, name } = ctx.user
        // console.log(id, name)
        // 2.派发令牌token
        try {
            const token = jwt.sign({ id, name }, PRIVATE_KEY, {
                expiresIn: 24 * 60 * 60,
                algorithm: 'RS256'
            })
            // console.log(token)
            // 3.返回用户信息
            ctx.body = { code: 0, data: { id, name, token } }
        } catch (error) {
            console.error('Error signing JWT:', error)
            ctx.body = { code: -1, message: 'Error signing JWT' }
        }
        // console.log(token)
    }
    test(ctx) {
        ctx.body = '身份验证成功'
    }

}
module.exports = new LoginController